package com.it.tydic.estate.module.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.it.tydic.estate.common.aop.CodeMsg;
import com.it.tydic.estate.common.aop.Result;
import com.it.tydic.estate.common.cache.DictionaryCache;
import com.it.tydic.estate.common.util.CommonUtils;
import com.it.tydic.estate.module.mapper.inner.*;
import com.it.tydic.estate.module.model.Dto.OccupancyDto;
import com.it.tydic.estate.module.model.Vo.Occupancy;
import com.it.tydic.estate.module.model.Vo.TbRoomTree;
import com.it.tydic.estate.module.model.inner.Fangjianguanli;
import com.it.tydic.estate.module.model.inner.Guanlichu;
import com.it.tydic.estate.module.model.inner.Louyu;
import com.it.tydic.estate.module.service.OccupancyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: liuhui
 * @createDate: 2020/3/4
 * @version: 1.0
 */
@Service
@Transactional
public class OccupancyServiceImpl implements OccupancyService {
    @Autowired
    private OccupancyMapper occupancyMapper;    /*数据综合*/
    @Autowired
    private TbHistoryMapper tbHistoryMapper;    /*历史*/
    @Resource
    private GuanlichuMapper guanlichuMapper;    /*管理处*/
    @Autowired
    private TbCustomerProfileMapper tbCustomerProfileMapper;    /*档案*/
    @Autowired
    private FangjianguanliMapper fangjianguanliMapper;  /*房间*/
    @Autowired
    private LouyuMapper louyuMapper;//楼宇


    /**
     * 入住统计
     * @param map
     * @return
     */
    @Override
    public Result selectByLike(Map<String, Object> map) {
        /*分页*/
//        if(map.containsKey("pageNum") && map.containsKey("pageSize")){//判断是否有分页条件有进行分页
//            PageHelper.startPage((Integer) map.get("pageNum"), (Integer) map.get("pageSize"));
//        }
        CommonUtils.pageProcess(map);//验证处理分页
        Page<Occupancy> occupancies = occupancyMapper.selectByLikeOnOccupancy(map);
        List<Occupancy> result = occupancies.getResult();
        /*设置基数*/
        int countOne = 0;
        int countTwo = 0;
        int countThere = 0;
        int countFour = 0;
        int countFive = 0;
        int countSix = 0;
//        循环赋值
        if(result != null && result.size()>0){
            for (Occupancy occupancy : result) {
                Guanlichu guanlichu = guanlichuMapper.selectByPrimaryKey(occupancy.getXqid());
                occupancy.setName(guanlichu.getName());
                /*添加总计数据*/
                countOne+=occupancy.getRoomNum();
                countTwo+=occupancy.getOwnerNum();
                countThere+=occupancy.getTenantNum();
                countFour+=occupancy.getVacantNum();
                /*获取迁入迁出数*/
                List<String> fj = occupancyMapper.getFj(guanlichu.getId());
                int moveIn = occupancyMapper.selectInCount(fj);
                int moveOut = occupancyMapper.selectOutCount(fj);
                countFive+=moveIn;
                countSix+=moveOut;
                occupancy.setMoveInNum(moveIn);
                occupancy.setMoveOutNum(moveOut);
            }
            /*获取合计数据*/
            Occupancy occupancyCount = new Occupancy();
            occupancyCount.setName("合计：");
            occupancyCount.setRoomNum(countOne);
            occupancyCount.setOwnerNum(countTwo);
            occupancyCount.setTenantNum(countThere);
            occupancyCount.setVacantNum(countFour);
            occupancyCount.setMoveInNum(countFive);
            occupancyCount.setMoveOutNum(countSix);
            result.add(occupancyCount);
        }
        /*返回集合*/
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("result",result);
        resultMap.put("totle",occupancies.getTotal());
        return Result.success(resultMap);
    }

    /**
     * 入住历史
     * @param map
     * @return
     */
    @Override
    public Result selectCheckInHistory(Map<String, Object> map) {
        /*分页*/
//        if(map.containsKey("pageNum") && map.containsKey("pageSize")){//判断是否有分页条件有进行分页
//            PageHelper.startPage((Integer) map.get("pageNum"), (Integer) map.get("pageSize"));
//        }
        CommonUtils.pageProcess(map);//验证处理分页
        Page<OccupancyDto> occupancyDtos = occupancyMapper.selectCheckInHistory(map);
        List<OccupancyDto> result = occupancyDtos.getResult();
        if(result.size()> 0 && result != null){
            /*码表赋值*/
            for (OccupancyDto occupancyDto : result) {
                occupancyDto.setPaidobject(DictionaryCache.getDicVal(occupancyDto.getPaidobject()));
                occupancyDto.setMType(DictionaryCache.getDicVal(occupancyDto.getMType()));
            }
        }
        /*返回集合*/
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("result",result);
        resultMap.put("totle",occupancyDtos.getTotal());
        return Result.success(resultMap);
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @Override
    public Result deleteByIds(List<String>ids) {
        /*验证参数*/
        if (ids.size() == 0)
            return  Result.error(CodeMsg.EMPTY_PARAM_ERROR);    /*参数为空*/
        return tbHistoryMapper.deleteByList(ids) == ids.size() ? Result.success(): Result.error(CodeMsg.DELETE_ERROR);
    }

    /**
     * 客户概况 车位
     * @param map
     * @return
     */
    @Override
    public Result selectOccupancyCw(Map<String, Object> map) {
        /*分页*/
//        if(map.containsKey("pageNum") && map.containsKey("pageSize")){//判断是否有分页条件有进行分页
//            PageHelper.startPage((Integer) map.get("pageNum"), (Integer) map.get("pageSize"));
//        }
        CommonUtils.pageProcess(map);//验证处理分页
        Page<Map<String, Object>> maps = occupancyMapper.selectOccupancyCw(map);
        List<Map<String, Object>> result = maps.getResult();
        /*返回集合*/
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("result",result);
        resultMap.put("totle",maps.getTotal());
        return Result.success(resultMap);
    }

    /**
     * 房间树
     * @param userCode
     * @return
     */
    @Override
    public Result getRoomTree(String userCode) {
        List<Guanlichu> glc = tbCustomerProfileMapper.getGLC(userCode);
        if(glc.size() > 0){
            List<TbRoomTree> glcList = new ArrayList<>();
            for (Guanlichu guanlichu : glc) {
                TbRoomTree glcTree = new TbRoomTree();
                glcTree.setName(guanlichu.getName());
                glcTree.setId(guanlichu.getId());
                Map<String, Object> map = new HashMap<>();
                map.put("xqid",guanlichu.getId());
                List<Louyu> result = louyuMapper.selectList(map).getResult();
                if(result.size()>0){
                    List<TbRoomTree> lyList = new ArrayList<>();
                    for (Louyu louyu : result) {
                        TbRoomTree lyTree = new TbRoomTree();
                        lyTree.setId(louyu.getId());
                        lyTree.setName(louyu.getLymc());
                        Map<String, Object> fjMap = new HashMap<>();
                        fjMap.put("lyid",louyu.getId());
                        /*获取房间信息*/
                        List<Fangjianguanli> fj = fangjianguanliMapper.selectList(fjMap).getResult();
                        if(fj.size()>0){
                            /*单个楼宇下所有的房间信息*/
                            List<TbRoomTree> fjList = new ArrayList<>();
                            for (Fangjianguanli fangjianguanli : fj) {
                                TbRoomTree fjTree = new TbRoomTree();
                                fjTree.setName(fangjianguanli.getFjbh());
                                fjTree.setId(fangjianguanli.getId());
                                fjTree.setFjbh(fangjianguanli.getFjbh());
                                fjTree.setGlcId(guanlichu.getId());
                                fjList.add(fjTree);
                            }
                            /*添加单个楼宇下的所有房间*/
                            lyTree.setObjectList(fjList);
                        }
                        /*添加每一个楼宇信息*/
                        lyList.add(lyTree);
                    }
                    /*添加所有楼宇数据*/
                    glcTree.setObjectList(lyList);
                }
                /*添加每一个管理处数据*/
                glcList.add(glcTree);
            }
            return glcList.size()> 0 ? Result.success(glcList):Result.error(CodeMsg.EMPTY_PARAM_ERROR);
        }
        return Result.error(CodeMsg.EMPTY_PARAM_ERROR);
    }

}
